﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BMS.Models;
using BMS.Models.BusinessLogic;
using BMS.Models.DataAccess;
using BMS.Utilities;
using BMS.ViewModels;


namespace BMS.Controllers
{
    public class ReportController : Controller
    {
        private OrderRepository orderRepository = new OrderRepository();
        private GeneralRepository generalRepository = new GeneralRepository();
        private ReportModelList rptList = new ReportModelList();
        private InvoiceRepository invoiceRepository = new InvoiceRepository();
        private QuotationRepository quotationRepository = new QuotationRepository();
        private ReportDocument rptDocument = new ReportDocument();

        private ReportRepository reportRepository = new ReportRepository();
        //
        // GET: /Report/

        public ActionResult Index()
        {
            ViewBag.Message = "Report";
            //rptModelList.ReportTypeList = generalRepository.GetReportTypesForDropDownList(Configuration.ReportTypeList);

            return View();
        }
        
        [HttpPost]
        public ActionResult ReportExtraction()
        {
            ViewBag.Message = "Report Confirmation";

            List<ReportModel> rptModelList = reportRepository.GetAllReportRecords();
            List<ReportModel> excelRptRecordsList = rptDocument.GetRecordsFromFile(Configuration.ReportPath);
            int StartRowNo = 2, EndRowNo = rptModelList.Count + 1;
            bool isSuccess = false;

            // Check whether the report needs to be updated or not
            if(excelRptRecordsList.Count > 0)
            {
                // Split into 2 lists (a) Existing Records List for Update & (b) New Records
                // (a) Retrieve the existing records from both sources
                List<ReportModel> existedRecordsList = reportRepository.GetExistingRecordsFromSources(rptModelList, excelRptRecordsList);
                // Find the records need to be updated
                List<ReportModel> updatedRecordsList = reportRepository.GetUpdatedRecordsFromSources(existedRecordsList, excelRptRecordsList);
                if(updatedRecordsList.Count > 0)
                {
                    isSuccess = rptDocument.UpdateToReport(Configuration.ReportPath, updatedRecordsList);
                }

                // (b) Retrieve new records from both sources
                List<ReportModel> newRecordsList = reportRepository.GetNewRecordsFromSources(rptModelList, existedRecordsList);
                if (newRecordsList.Count > 0)
                {
                    StartRowNo = rptDocument.GetRowsNoFromFile(Configuration.ReportPath) + 1;
                    EndRowNo = rptModelList.Count + 1;
                    isSuccess = rptDocument.WriteToReport(Configuration.ReportPath, newRecordsList, StartRowNo, EndRowNo);
                }
            }
            else
            {
                // All records are new records and add the new records into the report
                isSuccess = rptDocument.WriteToReport(Configuration.ReportPath, rptModelList, StartRowNo, EndRowNo);
            }
            rptList.isSuccess = isSuccess;

            return View(rptList);
        }
    }
}
